/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.listening.works.service.common;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.listening.framework.base.service.ServiceSupport;
import com.listening.framework.exception.BusinessException;
import com.listening.framework.utils.Digests;
import com.listening.framework.utils.Encodes;
import com.listening.framework.utils.Page;
import com.listening.framework.utils.Pagination;
import com.listening.works.domain.common.UserDomain;
import com.listening.works.entity.common.User;
import com.listening.works.repository.common.UserMybatisDao;

@Service()
@Transactional
public class UserService implements ServiceSupport<UserDomain, User, String> {
	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;

	@Autowired
	private UserMybatisDao userMybatisDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.listening.framework.base.service.ServiceSupport#insert(java.lang.
	 * Object, java.io.Serializable)
	 */
	public String insert(UserDomain domain, String id) throws BusinessException {
		entryptPassword(domain);

		User user = new User();
		BeanUtils.copyProperties(domain, user);
		return userMybatisDao.insert(user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.listening.framework.base.service.ServiceSupport#delete(java.io.
	 * Serializable)
	 */
	public Integer delete(String id) throws BusinessException {
		return userMybatisDao.delete(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.listening.framework.base.service.ServiceSupport#modify(java.lang.
	 * Object)
	 */
	public Integer modify(UserDomain domain) throws BusinessException {
		User user = new User();
		BeanUtils.copyProperties(domain, user);
		return userMybatisDao.modify(user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.listening.framework.base.service.ServiceSupport#select(java.lang.
	 * Object)
	 */
	@Override
	public List<UserDomain> select(UserDomain domain) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.listening.framework.base.service.ServiceSupport#selectByPK(java.io
	 * .Serializable)
	 */
	public UserDomain selectByPK(String id) throws BusinessException {
		// TODO Auto-generated method stub
		User userEntity = userMybatisDao.findOne(id);
		UserDomain userDomain = new UserDomain();
		BeanUtils.copyProperties(userEntity, userDomain);
		return userDomain;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.listening.framework.base.service.ServiceSupport#selectPageList(com
	 * .listening.framework.utils.Page, java.lang.Object)
	 */
	public Pagination<UserDomain> selectPageList(Page page, UserDomain domain) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 加密生成密码
	 * 
	 * @param userDomain
	 */
	private void entryptPassword(UserDomain userDomain) {
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		userDomain.setSalt(Encodes.encodeHex(salt));

		byte[] hashPassword = Digests.sha1(userDomain.getPlainPassword().getBytes(), salt, HASH_INTERATIONS);
		userDomain.setPassword(Encodes.encodeHex(hashPassword));
	}
}
